package com.lb.jwitter.action;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;

import com.lb.jwitter.service.ConditionReport;
import com.lb.jwitter.service.TblQLDTTXADMNhom;
import com.lb.jwitter.service.TblQLDTTXADMNhomService;
import com.lb.jwitter.service.TblQLDTTXADMPhong;
import com.lb.jwitter.service.TblQLDTTXADMPhongService;
import com.lb.jwitter.service.TblQLDTTXADMPhongThi;
import com.lb.jwitter.service.TblQLDTTXADMPhongThiService;
import com.lb.jwitter.service.TblQLDTTXADMTietHoc;
import com.lb.jwitter.service.TblQLDTTXADMTietHocService;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNam;
import com.lb.jwitter.service.TblQLDTTXAHocKyTrongNamService;
import com.lb.jwitter.service.TblQLDTTXALichThi;
import com.lb.jwitter.service.TblQLDTTXALichThiService;
import com.lb.jwitter.service.TblQLDTTXALopMonHoc;
import com.lb.jwitter.service.TblQLDTTXALopMonHocHocVienService;
import com.lb.jwitter.service.TblQLDTTXALopMonHocService;
import com.lb.jwitter.service.TblQuyenChiTietService;
import com.lb.jwitter.service.TblTaiKhoan;
import com.lb.jwitter.util.ApplicationContextProvider;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

@ContextConfiguration(locations="classpath:beans.xml") 
public class TblQLDTTXALichThiFormAction {
	private List<TblQLDTTXAHocKyTrongNam> tblQLDTTXAHocKyTrongNams; 
    public List<TblQLDTTXAHocKyTrongNam>  getTblQLDTTXAHocKyTrongNams(){ 
              return tblQLDTTXAHocKyTrongNams; 
     } 

    private TblQLDTTXAHocKyTrongNamService tblQLDTTXAHocKyTrongNamService; 
    
    private TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam;
    public TblQLDTTXAHocKyTrongNam getTblQLDTTXAHocKyTrongNam() {
    	return tblQLDTTXAHocKyTrongNam;
    }

    public void setTblQLDTTXAHocKyTrongNam(
    		TblQLDTTXAHocKyTrongNam tblQLDTTXAHocKyTrongNam) {
    	this.tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNam;
    }
    
    private List<TblQLDTTXADMPhongThi> tblQLDTTXADMPhongs; 
    public List<TblQLDTTXADMPhongThi>  getTblQLDTTXADMPhongs(){ 
              return tblQLDTTXADMPhongs; 
     } 
    
    private TblQLDTTXADMPhongThiService tblQLDTTXADMPhongThiService; 
    
    private List<TblQLDTTXADMTietHoc> tblQLDTTXADMTietHocs; 
    public List<TblQLDTTXADMTietHoc>  getTblQLDTTXADMTietHocs(){ 
              return tblQLDTTXADMTietHocs; 
     } 

    private TblQLDTTXADMTietHocService tblQLDTTXADMTietHocService; 
    
    private List<TblQLDTTXADMNhom> tblQLDTTXADMNhoms;
    public List<TblQLDTTXADMNhom> getTblQLDTTXADMNhoms(){
    	return tblQLDTTXADMNhoms;    	
    }
    
    private TblQLDTTXADMNhomService tblQLDTTXADMNhomService;
    
    private List<TblQLDTTXALopMonHoc> tblQLDTTXALopMonHocs;
    public List<TblQLDTTXALopMonHoc> getTblQLDTTXALopMonHocs(){
    	return tblQLDTTXALopMonHocs;    	
    }
    
    private TblQLDTTXALopMonHocService tblQLDTTXALopMonHocService;
    
    private List<TblQLDTTXALichThi> tblQLDTTXALichThis;
    public List<TblQLDTTXALichThi> getTblQLDTTXALichThis(){
    	return tblQLDTTXALichThis;    	
    }
    
    private TblQLDTTXALichThiService tblQLDTTXALichThiService;
    
    private TblQLDTTXALopMonHocHocVienService tblQLDTTXALopMonHocHocVienService;
    
    private Long hocKyId;
    public Long getHocKyId() {
    	return hocKyId;
    }

    public void setHocKyId(Long hocKyId) {
    	this.hocKyId = hocKyId;
    }

    
    private TblTaiKhoan taiKhoanDangNhap; 
    public TblTaiKhoan getTaiKhoanDangNhap() { 
        return taiKhoanDangNhap; 
    } 
    public void setTaiKhoanDangNhap(TblTaiKhoan taiKhoanDangNhap) { 
        this.taiKhoanDangNhap = taiKhoanDangNhap; 
    } 
    private TblQuyenChiTietService tblQuyenChiTietService;
	 @Autowired // Injected by Spring 
    ApplicationContextProvider contextProvider; 
public String execute() throws Exception 
{ 
	try{ 	
	     Map session = ActionContext.getContext().getSession();
	     taiKhoanDangNhap =  (TblTaiKhoan) session.get("taiKhoan");
	     if( taiKhoanDangNhap == null) return "noprivilege";
	    ApplicationContext context = ApplicationContextProvider.getApplicationContext(); 
	    tblQuyenChiTietService = (TblQuyenChiTietService) context.getBean("tbl_Quyen_ChiTietClient"); 
	    if(!tblQuyenChiTietService.checkRight(taiKhoanDangNhap.getMaNhomQuyen().getId(), "tbl_QLDTTXA_LichThi", "xem")){ 
	        return "noprivilege"; 
	    }
	    
	    tblQLDTTXADMPhongThiService = (TblQLDTTXADMPhongThiService) context.getBean("tbl_QLDTTXA_DM_PhongThiClient"); 

        tblQLDTTXADMTietHocService = (TblQLDTTXADMTietHocService) context.getBean("tbl_QLDTTXA_DM_TietHocClient"); 

        tblQLDTTXAHocKyTrongNamService = (TblQLDTTXAHocKyTrongNamService) context.getBean("tbl_QLDTTXA_HocKyTrongNamClient"); 

        tblQLDTTXADMNhomService = (TblQLDTTXADMNhomService) context.getBean("tbl_QLDTTXA_DM_NhomClient");
        
        tblQLDTTXALopMonHocService = (TblQLDTTXALopMonHocService) context.getBean("tbl_QLDTTXA_LopMonHocClient");
        
        tblQLDTTXALichThiService = (TblQLDTTXALichThiService) context.getBean("tbl_QLDTTXA_LichThiClient");
        
        tblQLDTTXALopMonHocHocVienService = (TblQLDTTXALopMonHocHocVienService) context.getBean("tbl_QLDTTXA_LopMonHoc_HocVienClient");
        
        tblQLDTTXADMPhongs = tblQLDTTXADMPhongThiService.findAlltblQLDTTXADMPhongThi(0, 0);
        tblQLDTTXADMTietHocs = tblQLDTTXADMTietHocService.findAlltblQLDTTXADMTietHoc(0, 0);
        
        tblQLDTTXADMNhoms = tblQLDTTXADMNhomService.findAlltblQLDTTXADMNhom(0, 0);
        
        Date today = new Date();
        List<ConditionReport> cons = new ArrayList<ConditionReport>(); 
        
        if(hocKyId != null && !hocKyId.equals(new Long(0))){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("id");
            con.setValue1(hocKyId);
            con.setOperator("eq");
            cons.add(con);	
        } else{
        	DateFormat df = new SimpleDateFormat("dd/MM/yyyy"); 

        	ConditionReport con = new ConditionReport(); 
            con.setCol("thoiGianBatDau");
            con.setValue1(df.format(today));
            con.setOperator("le");
            con.setType("date");
            cons.add(con);	
            
            ConditionReport con2 = new ConditionReport(); 
            con2.setCol("thoiGianKetThuc");
            con2.setValue1(df.format(today));
            con2.setOperator("ge");
            con2.setType("date");
            cons.add(con2);	
        }
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findtblQLDTTXAHocKyTrongNamByCondition(cons, 0, 0);
        
        if(tblQLDTTXAHocKyTrongNams != null && tblQLDTTXAHocKyTrongNams.size() ==1){ 
            tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
        }
        tblQLDTTXAHocKyTrongNams = tblQLDTTXAHocKyTrongNamService.findAlltblQLDTTXAHocKyTrongNam(0, 0);
        
        if(tblQLDTTXAHocKyTrongNam == null){
        	tblQLDTTXAHocKyTrongNam = tblQLDTTXAHocKyTrongNams.get(0);
        }
        
        cons.clear();
        if (true){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("hocKyId.id");
            con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
            con.setOperator("eq");
            cons.add(con);	
        }
        
        tblQLDTTXALopMonHocs = tblQLDTTXALopMonHocService.findtblQLDTTXALopMonHocByCondition(cons, 0, 0);
        
        cons.clear();
        if (true){
        	ConditionReport con = new ConditionReport(); 
            con.setCol("lopId.hocKyId.id");
            con.setValue1(tblQLDTTXAHocKyTrongNam.getId());
            con.setOperator("eq");
            cons.add(con);	
        }
        tblQLDTTXALichThis = tblQLDTTXALichThiService.findtblQLDTTXALichThiByCondition(cons, 0, 0);
        /*if (tblQLDTTXALichKiemTraGKs == null || tblQLDTTXALichKiemTraGKs.size() == 0) {
        	for (TblQLDTTXALopMonHoc tblQLDTTXALopMonHoc : tblQLDTTXALopMonHocs){
        		cons.clear();
        		if (true){
                	ConditionReport con = new ConditionReport(); 
                    con.setCol("lopMonHocId.id");
                    con.setValue1(tblQLDTTXALopMonHoc.getId());
                    con.setOperator("eq");
                    cons.add(con);	
                }
        		List<TblQLDTTXALopMonHocHocVien> tblQLDTTXALopMonHocHocViens = tblQLDTTXALopMonHocHocVienService.findtblQLDTTXALopMonHocHocVienByCondition(cons, 0, 0);
        		List<Long> nhomIds = new ArrayList();
        		for (TblQLDTTXALopMonHocHocVien tblQLDTTXALopMonHocHocVien : tblQLDTTXALopMonHocHocViens){
        			if (!nhomIds.contains(tblQLDTTXALopMonHocHocVien.getNhomId().getId())){
        				nhomIds.add(tblQLDTTXALopMonHocHocVien.getNhomId().getId());
        				TblQLDTTXALichKiemTraGK lichKT = new TblQLDTTXALichKiemTraGK();
        				lichKT.setLopId(tblQLDTTXALopMonHoc);
        				lichKT.setNhomId(tblQLDTTXALopMonHocHocVien.getNhomId());
        				Long newId = tblQLDTTXALichKiemTraGKService.createtblQLDTTXALichKiemTraGK(lichKT);
        				tblQLDTTXALichKiemTraGKs.add(tblQLDTTXALichKiemTraGKService.gettblQLDTTXALichKiemTraGK(newId));
        			}
        		}
        	}
        }*/
        return Action.SUCCESS; 
}
	catch(Exception e){ 
	      System.out.println("Loi Form tbl_QLDTTXA_LichThi Action"); 
	     e.printStackTrace(); 
	    return Action.ERROR; 
	 } 
}
}
